﻿# 输出开始提示
Write-Host "开始修复 OMEN Command Center 应用..." -ForegroundColor Cyan

# 1. 查找 OMEN Command Center 的安装路径
Write-Host "正在查找 OMEN Command Center 安装路径..." -ForegroundColor Yellow

$Package = Get-AppxPackage | Where-Object { $_.Name -like "*OMENCommandCenter*" }

# 检查是否找到了应用
if ($Package) {
    Write-Host "找到 OMEN Command Center，安装路径为：$($Package.InstallLocation)" -ForegroundColor Green
} else {
    Write-Host "未找到 OMEN Command Center，请检查是否已安装该应用。" -ForegroundColor Red
    exit 1  # 如果找不到应用，退出脚本
}

# 2. 终止 OMEN Command Center 进程
Write-Host "正在检查 OMEN Command Center 是否在运行..." -ForegroundColor Yellow

# 使用 taskkill 命令来停止进程
$processName = "Omen*"
$taskkillCommand = "taskkill /IM $processName /F"

# 执行 taskkill 命令
$processCheck = Invoke-Expression $taskkillCommand

if ($processCheck -match "SUCCESS") {
    Write-Host "OMEN Command Center 已被终止。" -ForegroundColor Green
} else {
    Write-Host "需根据提示信息确定 OMEN Command Center 未在运行或无法终止。" -ForegroundColor Red
}

# 3. 重新注册应用（保留数据）
Write-Host "正在重新注册 OMEN Command Center 应用..." -ForegroundColor Yellow

try {
    Add-AppxPackage -DisableDevelopmentMode -Register "$($Package.InstallLocation)\AppxManifest.xml" -Verbose
    Write-Host "OMEN Command Center 应用已成功重新注册。" -ForegroundColor Green
} catch {
    Write-Host "重新注册 OMEN Command Center 时发生错误：$_.Exception.Message" -ForegroundColor Red
    exit 1  # 如果注册失败，退出脚本
}

# 4. 启动 OMEN Command Center
Write-Host "正在启动 OMEN Command Center..." -ForegroundColor Yellow

try {
    Start-Process "OMENCommandCenter:"
    Write-Host "OMEN Command Center 启动成功。" -ForegroundColor Green
} catch {
    Write-Host "启动 OMEN Command Center 时发生错误：$_.Exception.Message" -ForegroundColor Red
    exit 1  # 如果启动失败，退出脚本
}

Write-Host "修复过程完成！" -ForegroundColor Cyan